﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc.Html
{
	public static class TableExtensions
	{
		public static object HtmlAttributes { get; set; }

		static TableExtensions()
		{
			HtmlAttributes = new
				                 {
									 @class = "table table-bordered table-hover table-striped"
				                 };
		}

		public static MvcTable BeginTable(this HtmlHelper htmlHelper)
		{
			return BeginTable(htmlHelper, null);
		}

		public static MvcTable BeginTable(this HtmlHelper htmlHelper, object htmlAttributes)
		{
			var table = new TagBuilder("table");
			table.MergeAttributes(HtmlAttributes.ToDictionary());
			table.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

			htmlHelper.ViewContext.Writer.Write(table.ToString(TagRenderMode.StartTag));

			return new MvcTable(htmlHelper.ViewContext);
		}

		public static void EndForm(this HtmlHelper htmlHelper)
		{
			htmlHelper.ViewContext.Writer.Write("</table>");
		}
	}
}